<?php
    include 'connectdb.php';
?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#date1').datepicker({
            dateFormat : 'yy-mm-dd'
        });
    });

    $(document).ready(function() {
        $('#date2').datepicker({
            dateFormat : 'yy-mm-dd'
        });
    });

function CheckDate(){
        var myDate1 = document.getElementById('date1').value;
        myDate1=myDate1.split("-");
        var newDate1=myDate1[0]+"-"+myDate1[1]+"-"+myDate1[2];
        var d1 = new Date(newDate1).getTime() / 1000;

        var myDate2=document.getElementById('date2').value;
        myDate2=myDate2.split("-");
        var newDate2=myDate2[0]+"-"+myDate2[1]+"-"+myDate2[2];
        var d2 = new Date(newDate2).getTime() / 1000;
        var d3 = ((((d2 - d1) / 60) / 60) / 24);
        document.getElementById('hiddenDate').value = d3;

}

function HomeStayInformation(){
    if(document.getElementById('home_stay_name').value != 0){
        window.location = "index.php?home_stay_name="+document.getElementById('home_stay_name').value;
    }
    
}
</script>

<form name="frmCheckDate" method="post" action="index.php" onSubmit="JavaScript:return CheckDate();">
<table border="1" width="260">
    <tr>
        <td>
            [เลือกสถานที่]
        </td>
    </tr>

    <tr>
        <td height="35">
                <select id="home_stay_name" name="home_stay_name" style="width :250px" onChange="JavaScript:HomeStayInformation();">
                <?php 
                    $h_id = $_REQUEST['home_stay_name'];
                    $q = "select * from home_stay";
                    $result = mysqli_query($link, $q);
                    while ($row = mysqli_fetch_array($result))
                    {
                        $selected = ($h_id == $row['home_stay_id']) ? ' selected' : '';
                        echo '<option value='.$row['home_stay_id'].$selected.'>'.$row['home_stay_name'].'</option>';
                    }
                    mysqli_close($link);
                ?>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            Check-in&nbsp;:</br>
            <?php
                if(isset($_POST['date1'])){
                    echo '<input name="date1" id="date1" value='.$_POST["date1"].' style="width :250px">';
                }
                else{
                    echo '<input name="date1" id="date1" value='.date("Y-m-d").' style="width :250px">';
                }
            ?>
        </td>
    </tr>
    <tr>
        <td>
            Check-out&nbsp;:</br>
            <?php
                if(isset($_POST['date2'])){
                    echo '<input name="date2" id="date2" value='.$_POST["date2"].' style="width :250px">';
                }
                else{
                    echo '<input name="date2" id="date2" value='.date("Y-m-d",  strtotime("+1 day")).' style="width :250px">';
                }
            ?>
            
            <input type="hidden" name="hiddenDate" id="hiddenDate"/>
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" name="btnCheckDate" value="ตรวจสอบ" style="width :250px" class="btn btn-primary">
        </td>
    </tr>
</table>
</form>